package main;
import java.util.ArrayList;
import java.util.List;

public class ContingencyTable {
	private int f11; // a
	private int f10; // b
	private int f01; // c
	private int f00; // d
	
	public ContingencyTable(int f11, int f10, int f01, int f00) {
		this.f11 = f11;
		this.f10 = f10;
		this.f01 = f01;
		this.f00 = f00;
	}
//    Y   !Y  
//X  f11  f10   f1p
//!X f01  f00   f0p
//   fp1  fp0   N	
	public double f11() {
		return f11;
	}

	public double f10() {
		return f10;
	}

	public double f01() {
		return f01;
	}

	public double f00() {
		return f00;
	}
	
	public double f1p() {
		return f11 + f10;
	}
	
	public double f0p() {
		return f01 + f00;
	}
	
	public double fp1() {
		return f11 + f01;
	}
	
	public double fp0() {
		return f10 + f00;
	}
	
	public double N() {
		return f11 + f10 + f01 + f00;
	}
	
	public List<ContingencyTable> neighbors() {
		List<ContingencyTable> result = new ArrayList<ContingencyTable>();
		if (f11 > 0) {
			if (f10 < N()) result.add(new ContingencyTable(f11 - 1, f10 + 1, f01, f00));
			if (f01 < N()) result.add(new ContingencyTable(f11 - 1, f10, f01 + 1, f00));
			if (f00 < N()) result.add(new ContingencyTable(f11 - 1, f10, f01, f00 + 1));
		}
		if (f10 > 0) {
			if (f11 < N()) result.add(new ContingencyTable(f11 + 1, f10 - 1, f01, f00));
			if (f01 < N()) result.add(new ContingencyTable(f11, f10 - 1, f01 + 1, f00));
			if (f00 < N()) result.add(new ContingencyTable(f11, f10 - 1, f01, f00 + 1));
		}
		if (f01 > 0) {
			if (f11 < N()) result.add(new ContingencyTable(f11 + 1, f10, f01 - 1, f00));
			if (f10 < N()) result.add(new ContingencyTable(f11, f10 + 1, f01 - 1, f00));
			if (f00 < N()) result.add(new ContingencyTable(f11, f10, f01 - 1, f00 + 1));
		}
		if (f00 > 0) {
			if (f11 < N()) result.add(new ContingencyTable(f11 + 1, f10, f01, f00 - 1));
			if (f10 < N()) result.add(new ContingencyTable(f11, f10 + 1, f01, f00 - 1));
			if (f01 < N()) result.add(new ContingencyTable(f11, f10, f01 + 1, f00 - 1));
		}
		return result;
	}
	
	@Override
	public String toString() {
		return String.valueOf(f11) + '\t' + f10 + '\t' + f01 + '\t' + f00 + '\t' + f1p() + '\t' + fp1(); 
	}

}
